/**
 * ©2013-2015 Alan L. Rights Reserved.
 */
package org.freedom.railhunter.model;

import java.io.Serializable;
import org.codehaus.jackson.annotate.JsonIgnore;

/**
 *
 * @author Alan
 */
public class QueueCount implements Serializable {
    
    private int count;
    
    private String ticket;
    
    private boolean op_2;
    
    private int countT;
    
    private boolean op_1;
    
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getTicket() {
        return ticket;
    }

    public void setTicket(String ticket) {
        this.ticket = ticket;
    }

    public boolean isOp_2() {
        return op_2;
    }

    public void setOp_2(boolean op_2) {
        this.op_2 = op_2;
    }

    public int getCountT() {
        return countT;
    }

    public void setCountT(int countT) {
        this.countT = countT;
    }

    public boolean isOp_1() {
        return op_1;
    }

    public void setOp_1(boolean op_1) {
        this.op_1 = op_1;
    }
    
    // 剩余张数
    @JsonIgnore
    private int remainingCount;
    
    // 剩余无座张数
    @JsonIgnore
    private int remainingWZCount;
    
    // 排队人数是否超过余票张数
    @JsonIgnore
    private boolean queueMoreThanRemaining;
    
    // 余票信息
    @JsonIgnore
    private String message;
    
    /**
     *  解码
     * 
     * @param seatTypeValue
     * @param hasWZFixed 
     */
    public void decomposeTicketStr(String seatTypeValue, boolean hasWZFixed) {
        String remainingCountInfo = getRemainingCountInfo(seatTypeValue);
        String[] remainingInfoArray = remainingCountInfo.split(",");
        // 本次列车，剩余张数
        remainingCount = Integer.parseInt(remainingInfoArray[0]);
        message = "本次列车，剩余" + remainingCount + "张";
        boolean isWZRemaining = false;
        if (remainingInfoArray.length > 1) {
            // 无座张数
            remainingWZCount = Integer.parseInt(remainingInfoArray[1]);
            message = "本次列车，剩余无座" + remainingWZCount + "张";
            isWZRemaining = true;
        }
        
        if (isOp_2()) {
            if ((hasWZFixed && !isWZRemaining) || !hasWZFixed) {
                // 取无座
            } else {
                message = "目前排队人数已经超过余票张数，请您选择其他席别或车次。";
                queueMoreThanRemaining = true;
            }
        } else {
            // 目前排队人数
            message = "目前排队人数为" + getCountT();
            queueMoreThanRemaining = false;
        }
    }
    
    public String getRemainingCountInfo(String seatTypeValue) {
        String rt = "";
        int seat_1 = -1;
        int seat_2 = -1;
        int i = 0;
        while (i < ticket.length()) {
            String s = ticket.substring(i, i + 10);
            String c_seat = s.substring(0, 1);
            if (c_seat.equals(seatTypeValue)) {
                String c = s.substring(6, 10);
                while (c.length() > 1 && "0".equals(c.substring(0, 1))) {
                    c = c.substring(1);
                }
                int ci = Integer.parseInt(c);
                if (ci < 3000) {
                    seat_1 = ci;
                } else {
                    seat_2 = (ci - 3000);
                }
            }
            i = i + 10;
        }
        if (seat_1 > -1) {
            rt += seat_1;
        }
        if (seat_2 > -1) {
            rt += "," + seat_2;
        }
        return rt;
    }

    public int getRemainingCount() {
        return remainingCount;
    }

    public void setRemainingCount(int remainingCount) {
        this.remainingCount = remainingCount;
    }

    public int getRemainingWZCount() {
        return remainingWZCount;
    }

    public void setRemainingWZCount(int remainingWZCount) {
        this.remainingWZCount = remainingWZCount;
    }

    public boolean isQueueMoreThanRemaining() {
        return queueMoreThanRemaining;
    }

    public void setQueueMoreThanRemaining(boolean queueMoreThanRemaining) {
        this.queueMoreThanRemaining = queueMoreThanRemaining;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
